package com.go.where.family.component.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.go.where.family.component.CustomerAttentionComponent;
import com.go.where.family.constants.TransmissionCode;
import com.go.where.family.exception.ServerDisponseException;
import com.go.where.family.pojo.CustomerAttentionInfo;
import com.go.where.family.scoket.Head;
import com.go.where.family.service.CustomerAttentionService;
import com.go.where.family.service.CustomerCommentService;
import com.go.where.family.utils.CommonUtil;
import com.go.where.family.vo.CustomerAttentionVo;
import com.google.gson.Gson;

@Component("customerAttentionComponent")
public class CustomerAttentionComponentImpl implements
		CustomerAttentionComponent {

	private static final Logger log = Logger
			.getLogger(CustomerAttentionComponentImpl.class);
	private Gson gson;
	@Resource(name = "customerAttentionService")
	private CustomerAttentionService customerAttentionService;
	@Resource(name = "customerCommentService")
	private CustomerCommentService customerCommentService;

	public CustomerAttentionComponentImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public boolean save(String body, Head head) {
		CustomerAttentionVo vo = gson.fromJson(body, CustomerAttentionVo.class);
		if (vo != null) {
			String attentionOrNot = vo.getAttentionOrNot();
			CustomerAttentionInfo att = new CustomerAttentionInfo();
			att.setCustomerId(vo.getCustomerId());
			CustomerAttentionInfo cai = null;
			if (StringUtils.isNotBlank(vo.getFollowedId())) {
				att.setFollowedId(vo.getFollowedId());
				cai = customerAttentionService.queryParameter(att);

			}
			CustomerAttentionInfo cai2 = null;
			if (StringUtils.isNotBlank(vo.getActivityId())) {
				att.setFollowedId("");
				att.setActivityId(vo.getActivityId());
				cai2 = customerAttentionService.queryParameter(att);
			}
			int result = 0;
			att.setFollowedId(vo.getFollowedId());
			if ("0".equals(attentionOrNot)
					&& StringUtils.isNotBlank(vo.getFollowedId())) {
				if (cai != null) {
					att.setFollowedId(vo.getFollowedId());
					result = customerAttentionService.delete(att);
					return true;
				} else {
					throw new ServerDisponseException(
							TransmissionCode.CANCEL_NO_ATTENTION.getCode(),
							TransmissionCode.CANCEL_NO_ATTENTION.getMessage());
				}
			}
			if ("0".equals(attentionOrNot)
					&& StringUtils.isNotBlank(vo.getActivityId())) {
				if (cai2 != null) {
					result = customerAttentionService.delete(att);
					return true;
				} else {
					throw new ServerDisponseException(
							TransmissionCode.CANCEL_NO_ATTENTION.getCode(),
							TransmissionCode.CANCEL_NO_ATTENTION.getMessage());
				}
			}
			if ("1".equals(attentionOrNot) && (cai == null && cai2 == null)) {
				result = customerAttentionService.save(att);
			} else {
				throw new ServerDisponseException(TransmissionCode.ATTENTIONED
						.getCode(), TransmissionCode.ATTENTIONED.getMessage());
			}
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",保存客户关注信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean delete(String body, Head head) {
		CustomerAttentionVo vo = gson.fromJson(body, CustomerAttentionVo.class);
		if (vo != null) {
			CustomerAttentionInfo att = new CustomerAttentionInfo();
			att.setCustomerId(vo.getCustomerId());
			att.setFollowedId(vo.getFollowedId());
			att.setActivityId(vo.getActivityId());
			int result = customerAttentionService.delete(att);
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",取消客户关注信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public boolean deleteAll(String body, Head head) {
		CustomerAttentionVo vo = gson.fromJson(body, CustomerAttentionVo.class);
		if (vo != null) {
			CustomerAttentionInfo att = new CustomerAttentionInfo();
			att.setCustomerId(vo.getCustomerId());
			if ("1".equals(vo.getAttentionType())) {
				att.setActivityIdIsNull("1");
			} else {
				att.setFollowedIdIsNull("2");
			}
			int result = customerAttentionService.delete(att);
			if (result > 0) {
				return true;
			} else {
				return false;
			}
		} else {
			log.info("requestId:" + head.getRequestId() + ",删除全部关注信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

	@Override
	public List<CustomerAttentionInfo> queryList(String body, Head head) {
		CustomerAttentionVo vo = gson.fromJson(body, CustomerAttentionVo.class);
		if (vo != null) {
			Integer currentPage = vo.getPageIndex();
			Integer pageSize = vo.getPageCount();
			Integer pageStart = 1;
			Integer pageEnd = 10;
			if (currentPage != null && pageSize != null) {
				pageStart = (currentPage - 1) * pageSize + 1;
				pageEnd = currentPage * pageSize;
			}
			Map<String, Object> paraMap = new HashMap<String, Object>();
			paraMap.put("pageStart", pageStart);
			paraMap.put("pageEnd", pageEnd);
			paraMap.put("customerId", vo.getCustomerId());
			if ("1".equals(vo.getAttentionType())) {
				paraMap.put("activityIdIsNull", "1");
			} else {
				paraMap.put("followedIdIsNull", "2");
			}

			List<CustomerAttentionInfo> list = customerAttentionService
					.queryList(paraMap);
			if (list != null && !list.isEmpty()) {
				for (CustomerAttentionInfo att : list) {
					if (StringUtils.isNotBlank(att.getHeaderPath())) {
						att.setHeaderPath(CommonUtil.getUrlPrefix()
								+ att.getHeaderPath());
					}
					if (StringUtils.isNotBlank(att.getFollowedId())) {
						String lastOne = customerCommentService
								.queryLastOneComment(att.getFollowedId());
						att.setLastOneComment(lastOne);
					}
				}
				return list;
			} else {
				return new ArrayList<CustomerAttentionInfo>();
			}

		} else {
			log.info("requestId:" + head.getRequestId() + ",查询关注信息时，报文解析异常");
			throw new ServerDisponseException(TransmissionCode.ERROR_ADJUST
					.getCode(), TransmissionCode.ERROR_ADJUST.getMessage());
		}
	}

}
